/**
 * Created by wangcan on 2017/9/27.
 * 宝宝树66小区三期改造 拦截旧版本宝宝树66小区APP提示下载最新版本APP add
 */

var toolUtil = require('../common/utils/toolUtil');
var messageUtil = require('../common/utils/messageUtil');
var path = require('path');
var moment = require('moment');
// var system = require('system');

exports.appUpdate = function (req, res, next) {
    var sysDate = Date.now(); // 当前系统时间毫秒值
    // 新版本APP发布时间
    var appPublicDate = moment(newAppVersionPublicDate, "YYYY-MM-DD").format();
    appPublicDate = new Date(appPublicDate).getTime();
    // 如果新版本APP已发布，提示用户下载最新版本APP并禁用老版本APP
    if (sysDate < appPublicDate) {
        next();
    } else {
        var vCode = req.cparams.vcode;
        var vName = req.query.vname; // 版本号
        /*  宝宝树66小区四次改造-禁用旧版    mod   guo.xiaomin  20171102   start   */
        // 2个问号的话，就会出现vname=1.0.0-release?testkey1=12315的情况
        // 此处直接截取，无需配置文件修改
        if(vName && vName.length > 5) {
            vName = vName.substring(0, 5);
        }
        /*  宝宝树66小区四次改造-禁用旧版    mod   guo.xiaomin  20171102   end   */
        var clientType = req.query.type; // 终端系统类型：Android/iOS
        // var errorMsg = appUpdatePromptMsg;
        var url = req.originalUrl; // 取得客户端原始请求URL

        // 不需要拦截的URL
        var passUrlArr = [
            "/rest/" + RESTFullServiceVersion + "/app/marketZip",
            "/rest/" + RESTFullServiceVersion + "/app/splashScreen",
            "/rest/" + RESTFullServiceVersion + "/appLog/requestStatus",
            "/rest/" + RESTFullServiceVersion + "/masterData/cityList",
            "/rest/" + RESTFullServiceVersion + "/masterData/regionList",
            "/rest/" + RESTFullServiceVersion + "/masterData/userHobby",
            "/rest/" + RESTFullServiceVersion + "/masterData/userPortrait",
            "/rest/" + RESTFullServiceVersion + "/Systems/getAppConfig",
            "/rest/" + RESTFullServiceVersion + "/Systems/getQiniuToken",
            "/rest/" + RESTFullServiceVersion + "/users/checkToken/",
            "/rest/" + RESTFullServiceVersion + "/users/logout"
        ];
        var passFlg = false;
        // 过滤不需要拦截的客户端请求URL
        for (var i = 0, length = passUrlArr.length; i < length; i++) {
            if (url.indexOf(passUrlArr[i]) != -1) {
                // next();
                passFlg = true;
                break;
            }
        }
        if (!passFlg) {
            if (vName != null && newAppVersionPublic.indexOf(vName) == -1) {
                // 提示用户下载最新版本APP
                console.log("拦截的请求。。。。" + url);
                res.json(messageUtil.composeFailedMessage(req, appUpdatePromptMsg, 1059));
                return;
                // toolUtil.putMessageBusinessMQ({msgType: 'finish_task', desc: desc});
            } else {
                next();
            }
        } else {
            next();
        }
    }
};